package com.cloudkinto.controller.invoice;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.service.invoice.vo.InvoiceTitleAddReq;
import com.cloudkinto.service.invoice.vo.InvoiceTitleDetailRes;
import com.cloudkinto.service.invoice.vo.InvoiceTitleUpdateReq;
import com.cloudkinto.service.orderoutput.vo.OrderOutputDetailRes;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.invoice.InvoiceTitleService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hjy
 * @since 2021-07-13
 */
@RestController
@Api(tags = "发票抬头模块")
@RequestMapping("/api/admin/invoiceTitle")
public class InvoiceTitleController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(InvoiceTitleController.class);

    @Autowired
    private InvoiceTitleService service;

    @UserSessionRequired
    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @UserSessionRequired
    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增", response = InvoiceTitleAddReq.class)
    public SingleResult add(@RequestBody @Validated InvoiceTitleAddReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        if (companyId != null) {
            req.setCompanyId(companyId);
        }
        return SingleResult.success("新增成功", service.add(req, userId, companyId));
    }

    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @UserSessionRequired
    @GetMapping("/owe/list")
    @ApiOperation(value = "查询发票抬头和欠票信息", notes = "查询")
    public SingleResult oweList(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String companyId = request.getParameter("companyId");
        map.put("keyword", keyword);
        if (StringUtils.isNotBlank(companyId))
            map.put("companyId", companyId);
        return SingleResult.success("查询成功", service.oweList(map));
    }

    @UserSessionRequired
    @PutMapping("")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated InvoiceTitleUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(id, userId));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = InvoiceTitleDetailRes.class)})
    public SingleResult<InvoiceTitleDetailRes> detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "bank", value = "开户行"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    public SingleResult list(HttpServletRequest request) {
        Long cId = getCompanyId();
        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String companyId = request.getParameter("companyId");

        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        if (cId != null) {
            map.put("companyId", cId + "");
        } else if (StringUtils.isNotBlank(companyId)) {
            map.put("companyId", companyId);
        }
        map.put("current", current);
        map.put("pageSize", size);
        map.put("keyword", keyword);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "bank", value = "开户行"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "companyId", value = "公司id"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    public PageResult listPage(HttpServletRequest request) {
        Long cId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String companyId = request.getParameter("companyId");

        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        if (cId != null) {
            map.put("companyId", cId + "");
        } else if (StringUtils.isNotBlank(companyId)) {
            map.put("companyId", companyId);
        }
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }


}

